package ru.amse.podolsky.fractalarch.io;

/**
 * @author Алексей
 */

import ru.amse.podolsky.fractalarch.model.*;
import java.awt.image.*;
import java.io.*;

public class ArrayExtractor {
    private final int[][] array;
    
    public ArrayExtractor(int[][] array){
        this.array = array;
    }
    
    public Compression extractCompressFromArray() throws IOException{
       int imageWidth = this.array[0][2];
       int imageHeight = this.array[0][3];
       int middleColor = this.array[0][1];
       int step = this.array[0][4];
       
       BufferedImage buff = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
       
        for(int i = 0; i < imageWidth; ++i){
           for(int k = 0; k < imageHeight; ++k){
               buff.setRGB(i, k, (middleColor << 16) + (middleColor << 8) + middleColor);
               //buff.setRGB(i, k, (128 << 16) + (128 << 8) + 128);
           }
        }
        return new Compression(buff,toStr(step));
    }
    
    public int extractStepFromArray(){
        return this.array[0][4];
    }
    
    public DomenParams[] extractParamsFromArray(){
       int length =  this.array.length;
       DomenParams[] out = new DomenParams[length - 1];
       for(int i = 1; i < length; ++i){
           out[i-1] = new DomenParams();
       }
       //Если this.array[0][5] == 0, то это был текстовый архив
       if(this.array[0][5] == 0){
           for(int i = 1; i < length; ++i){
               out[i - 1].domenXCoord = this.array[i][0];
               out[i - 1].domenYCoord = this.array[i][1];
               out[i - 1].sameBlockNumber = this.array[i][2];
               out[i - 1].shift = this.array[i][3];
               out[i - 1].numberOfConversion = this.array[i][4];
               out[i - 1].testDelta = this.array[i][5];
           }
       //иначе - битовый
       }else{
           int curWidth = 0;
           int curHeigth = 0;
           int width = this.array[0][2];
           int heigth = this.array[0][3];
           int step = this.array[0][4];

           for(int i = 1; i < length; ++i){
               out[i - 1].sameBlockNumber = this.array[i][0];
               out[i - 1].shift = this.array[i][1];
               out[i - 1].numberOfConversion = this.array[i][2];
               out[i - 1].testDelta = 0;
               out[i - 1].domenXCoord = curWidth;
               out[i - 1].domenYCoord = curHeigth;

               if((curHeigth + IParams.DOMEN_SIZE) == heigth){
                   curHeigth = 0;
                   if(curWidth + step <= width - IParams.DOMEN_SIZE){
                       curWidth += IParams.DOMEN_SIZE;
                   }else{
                       curWidth = width - 2 * IParams.DOMEN_SIZE; 
                   }
               }else{
                   if((curHeigth + step) <= heigth - IParams.DOMEN_SIZE){
                       curHeigth += IParams.DOMEN_SIZE;
                   }else{                     
                       curHeigth = heigth - 2 * IParams.DOMEN_SIZE;
                   }
               }
           }
       }
       return out;
    }
    
    private String toStr(int it){
       StringBuilder sb = new StringBuilder();
       String out; 
       while(it != 0){
           sb.append(it % 10);
           it /= 10;
       }
       sb.reverse();
       out = sb.toString();
       return out;
   }
}
